package com.free.mini.core.serve.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.free.mini.core.serve.entity.StudyQuestion;
import com.free.mini.core.serve.mapper.StudyQuestionMapper;
import com.free.mini.core.serve.service.IStudyQuestionService;
import com.free.mini.core.server.vo.req.StudyQuestionCreateReqVO;
import com.free.mini.core.server.vo.req.StudyQuestionListQueryReqVO;
import com.free.mini.core.server.vo.req.StudyQuestionQueryDetailReqVO;
import com.free.mini.core.server.vo.req.StudyQuestionUpdateReqVO;
import com.free.mini.core.server.vo.resp.StudyQuestionListQueryRespVO;
import com.free.mini.core.server.vo.resp.StudyQuestionQueryDetailRespVO;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

/**
 * 学习问题表 Service实现
 *
 * @author dongsufeng
 * @date 2023-08-02 17:50:48
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class StudyQuestionServiceImpl extends ServiceImpl<StudyQuestionMapper, StudyQuestion> implements IStudyQuestionService {

    private final StudyQuestionMapper studyQuestionMapper;

    @Override
    public Page<StudyQuestionListQueryRespVO> page(StudyQuestionListQueryReqVO reqVO) {
        LambdaQueryWrapper<StudyQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(reqVO.getEnable() != null, StudyQuestion::getEnable, reqVO.getEnable());
        queryWrapper.eq(StringUtils.isNotBlank(reqVO.getModuleCode()), StudyQuestion::getModuleCode,reqVO.getModuleCode());
        Page<StudyQuestion> page = new Page<>(reqVO.getPageIndex(),reqVO.getPageSize());
        IPage<StudyQuestion> studyQuestionPage = this.page(page, queryWrapper);
        List<StudyQuestionListQueryRespVO> respVOList = studyQuestionPage.getRecords().stream().map(map ->
                BeanUtil.copyProperties(map, StudyQuestionListQueryRespVO.class)
        ).collect(Collectors.toList());
        Page<StudyQuestionListQueryRespVO> respVOPage = new Page<>();
        respVOPage.setRecords(respVOList);
        respVOPage.setCurrent(studyQuestionPage.getCurrent());
        respVOPage.setTotal(studyQuestionPage.getTotal());
        return respVOPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createStudyQuestion(StudyQuestionCreateReqVO reqVO) {
        StudyQuestion studyQuestion = BeanUtil.copyProperties(reqVO, StudyQuestion.class);
        studyQuestion.setSubTitle(reqVO.getSubTitle().length()>255 ? reqVO.getSubTitle().substring(255) : reqVO.getSubTitle());
        return this.save(studyQuestion);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateStudyQuestion(StudyQuestionUpdateReqVO reqVO) {
        StudyQuestion studyQuestion = BeanUtil.copyProperties(reqVO, StudyQuestion.class);
        return this.updateById(studyQuestion);
    }

    @Override
    public StudyQuestionQueryDetailRespVO detail(StudyQuestionQueryDetailReqVO reqVO) {
        LambdaQueryWrapper<StudyQuestion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudyQuestion::getId,reqVO.getQuestionId());
        StudyQuestion studyQuestion = this.getOne(queryWrapper);
        if (Objects.nonNull(studyQuestion)){
            return BeanUtil.copyProperties(studyQuestion, StudyQuestionQueryDetailRespVO.class);
        }
        return null;
    }
}
